var rldMap = null;//风险级别字典
/**
 * 通知管理初始化
 */
var PatienList = {
    id: "patientListTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
PatienList.initColumn = function () {
    var columnArr =  [
        {title: '院区', field: 'hospName', align: 'center', width:120,valign: 'middle', sortable: false},
        {title: '科室', field: 'curDept', align: 'center', width:90,valign: 'middle', sortable: false},
        {title: '患者姓名', field: 'caseName', align: 'center', width:60, valign: 'middle', sortable: false},
        {title: '病案号', field: 'caseMrno', align: 'center', width:70,valign: 'middle', sortable: false},
        {title: '入组病种', field: 'entityName', align: 'center', width:120,valign: 'middle', sortable: false},
        {title: '主治医生', field: 'docName', align: 'center', width:60, valign: 'middle', sortable: false},
        {title: '评估表名称', field: 'description', align: 'center', width:140,valign: 'middle', sortable: false},
        {title: '评估结果分值', field: 'score', align: 'center', width:50, valign: 'middle', sortable: false,
            formatter:function(value,row,index){
                return "<a onclick='PatienList.homeKeyDetail(&apos;"+row.evalId+"&apos;,&apos;"+row.episodeId+
                    "&apos;,&apos;"+row.asmId+"&apos;,&apos;"+row.entityId+"&apos;,&apos;"+row.description+"&apos;)'>"+value+"</a>";
            }},
        {title: '评估结果等级', field: 'level', align: 'center', width:70, valign: 'middle', sortable: false},
        {title: '评估时间', field: 'createDate', align: 'center', width:90, valign: 'middle', sortable: false}
    ];
    return columnArr;
};


/**
 * 检查是否选中
 */
PatienList.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        PatienList.seItem = selected[0];
        return true;
    }
};


/**
 * 导出数据-不同条件
 */
PatienList.exportExcel = function (){

    var startDate = $('#startDate').datebox('getValue');
    var endDate = $('#endDate').datebox('getValue');
    var queryData = {};
    queryData['assessmentType'] = $('#assessmentType').datebox('getValue');    //评估表名称
    queryData['startDate'] = startDate;    //获取页面开始时间
    queryData['endDate'] = endDate;    //获取页面结束时间
    queryData['office'] = $('#office').datebox('getValue');//部门
    queryData['depart'] = $('#depart').datebox('getValue');//科室
    queryData['entityType'] = $('#entityType').datebox('getValue');//病种类别
    queryData['entityId'] = $('#entityType').datebox('getValue');//病种ID

    if(queryData['startDate']!=""){
        queryData['startDate'] = queryData['startDate'] + " 00:00:00";
    }
    if(queryData['endDate']!=""){
        queryData['endDate'] = queryData['endDate'] + " 23:59:59";
    }
    var longStart = Date.parse(startDate);
    var longEnd = Date.parse(endDate);
    if(longStart>longEnd){
        Sys.error("开始日期不可大于结束日期");
        return false;
    }

    Sys.loading("数据文件生成中,请耐心等待");
    var ajax = new $ax(Sys.backendPath + "/sdc/case/createExcel", function(data){
        Sys.closeLoading();
        if(data.message=="DHCC"){
            Sys.info("无数据可以导出，请更改查询条件。");
        }else{
            window.location.href = Sys.backendPath + "/sdc/case/excelDown/"+data.message;
            PatienList.table.refresh();
        }
    },function(data){
        Sys.error("数据生成失败!" + data.responseJSON.message + "!");
    });
    ajax.set(queryData);
    ajax.setAsync(true);
    ajax.start();
}

/**
 * 查询病例
 */
PatienList.search = function (){
    var startDate = $('#startDate').datebox('getValue');
    var endDate = $('#endDate').datebox('getValue');
    var queryData = {};
    queryData['assessmentType'] = $('#assessmentType').datebox('getValue');    //评估表名称
    queryData['startDate'] = startDate;    //获取页面开始时间
    queryData['endDate'] = endDate;    //获取页面结束时间
    queryData['office'] = $('#office').datebox('getValue');//部门
    queryData['xcode'] = $('#xcode').val();//就诊号
    queryData['name'] = $('#name').val();//患者姓名
    queryData['mrNum'] = $('#mrNum').val();//病案号
    queryData['docName'] = $('#docName').val();//主治医生
    queryData['depart'] = $('#depart').datebox('getValue');//科室
    queryData['entityType'] = $('#entityType').datebox('getValue');//患者类别

    if(queryData['startDate']!=""){
        queryData['startDate'] = queryData['startDate'] + " 00:00:00";
    }
    if(queryData['endDate']!=""){
        queryData['endDate'] = queryData['endDate'] + " 23:59:59";
    }

    var longStart = Date.parse(startDate);
    var longEnd = Date.parse(endDate);
    if(longStart>longEnd){
        Sys.error("开始日期不可大于结束日期");
        return false;
    }
    PatienList.table.refresh({query: queryData,pageNumber: 1});
}

/**
 * //根据父级机构 查询子机构
 */
PatienList.officeChange = function(that){
    var ajax = new $ax(Sys.backendPath + "/sds/common/deptListByHospId", function(data){
        $('#depart').combobox({
            data:data,
            valueField:'value',
            textField:'text',
            defaultFilter: 6,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        layer.msg("查询失败!" + data.responseJSON.message + "!",{icon: 2});
    });
    ajax.set({"hospID":that.value,"id":"depart"});
    ajax.start();
}


$(function () {
    //科室
    $('#depart').combobox({
        data:$.parseJSON($("#departMap").val()),
        valueField:'value',
        textField:'text'
    });
    //院区
    $('#office').combobox({
        data:$.parseJSON($("#officeMap").val()),
        valueField:'value',
        textField:'text',
        defaultFilter: 6,
        onSelect:PatienList.officeChange,
        onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
            var val = $(this).combobox("getData");
            if(val.length==2){//如果就一个院区 加上全部=2
                $(this).combobox("setValue", val[1].value);
                PatienList.officeChange(val[1]);
            }else{
                $(this).combobox("setValue", "");
            }
        }
    });
    //病种
    $('#entityType').combobox({
        data:$.parseJSON($("#entityMap").val()),
        valueField:'value',
        textField:'text',
        defaultFilter: 6
    });
    // 评估表名称
    $('#assessmentType').combobox({
        data:$.parseJSON($("#assessmentMap").val()),
        valueField:'value',
        textField:'text',
        defaultFilter: 6
    });
    //渲染datebox --没有渲染的话 图标左右间距不等
    $('.hisui-datebox').datebox({});

    //默认365天
    $('#startDate').datebox('setValue', timeStamp2String(new Date().getTime()-(1000 * 60 * 60 * 24*365)));
    $('#endDate').datebox('setValue', timeStamp2String(new Date().getTime()));

    var startDate = $('#startDate').datebox('getValue');//开始日期
    var endDate = $('#endDate').datebox('getValue');//结束日期
    startDate = startDate + " 00:00:00";
    endDate = endDate + " 23:59:59";
    var assessmentType = $('#assessmentType').combobox('getValue');
    var entityType = $('#entityType').combobox('getValue');
    var defaultColunms = PatienList.initColumn();
    var table = new BSTable(PatienList.id, Sys.backendPath + "/sdc/case/evaluationList", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams({assessmentType:assessmentType,entityType:entityType,startDate:startDate,endDate:endDate,office:'',depart:''});
    table.setShowColumns(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setPageSize(15);
    table.setSubHeight(-27);

    PatienList.table = table.init();

    //绑定顶部和中部蓝色数字的点击事件
    $(".homeCaseDeatil").click(function(){
        var _this = $(this);
        var key = _this.attr("evalId");
        var episodeId = _this.attr("episodeId");
        var asmId = _this.attr("asmId");
        var entityId = _this.attr("entityId");
        var description = _this.attr("description");
        PatienList.homeKeyDetail(key,episodeId,asmId,entityId,description);
    });
});

/**
 * 钻取主页蓝色数字详细信息
 */
PatienList.homeKeyDetail = function(key,departCode, drgCode,desc, type){
        var href = Sys.backendPath + "/sdc/case/toEvaluationDetail?key="+key+"&patientId="+departCode+"&asmId="+drgCode+"&entityId="+desc+"&description="+type;

    layer.open({
        type: 2,
        title: ['<i class = "fa fa-list-ol"></i>&nbsp;'+type,'padding-left:20px'],
        area:["90%","90%"],
        scrollbar: false,//为了禁止父页面滚动条滚动
        skin:'classfyLBStyle layer-ext-hisui',
        content: href //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
    });
    //Sys.addTabPage($.trim(desc), href,true);
};

//在Jquery里格式化Date日期时间数据
function timeStamp2String(time){
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    return year + "-" + month + "-" + date;
}